﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace AdvanceLib.AddIn
{
    public class PluginLoader<TInterface>
    {
        #region Constructors
        public PluginLoader()
        {
            this.Plugins = new List<TInterface>();
        }
        #endregion

        #region Properties
        public List<TInterface> Plugins { get; set; }
        #endregion

        #region Methods
        public void LoadPlugins(Assembly asm)
        {
            try
            {
                Type[] types = asm.GetTypes();

                foreach (Type type in types)
                {
                    if (type.GetInterfaces().Contains(typeof(TInterface)))
                    {
                        this.Plugins.Add((TInterface)Activator.CreateInstance(type));
                    }
                }
            }
            catch
            {
                throw new Exception("Failed to load plugins from " + asm.FullName + ".");
            }
        }
        #endregion
    }
}
